<template>
	<view>
		<view class="block">
			<view class="title">
				我的账户
			</view>
			<view class="content">
				
				<view class="left">
					<view class="box">
						<view class="num">{{money.yk_static}}</view>
						<view class="text">金豆</view>
					</view>
					<view class="box">
						<view class="num">{{money.money}}</view>
						<view class="text">余额</view>
					</view>
					<view class="box">
						<view class="num">{{money.sc_money}}</view>
						<view class="text">抵扣券</view>
					</view>
				</view>
				
				<!-- <view class="my">
					我的账户余额：{{money.money||0}} 元
				</view>
				<view class="my">
					我的抵扣券：{{money.sc_money||0}} 元
				</view> -->
			</view>
		</view>
		<view class="block">
			<view class="title">
				充值金豆
			</view>
			<view class="content">
				<view class="amount">
					<view class="list">
						<view class="box" v-for="(amount,index) in amountList" :key="index" @tap="select(amount)" :class="{'on':amount == inputAmount}">
							{{amount}}个
						</view>
					</view>
					<!-- <view class="num">
						<view class="text">
							自定义充值金额
						</view>
						<view class="input">
							<input type="number" v-model="inputAmount" />
						</view>
					</view> -->
				</view>
			</view>
		</view>
		<view class="block">
			<view class="title">
				上传转账截图
			</view>
			<view class="content" style="padding-top: 10px;">
				<u-upload
					accept="image"
					:maxSize="4194304"
					:fileList="fileList1"
					@afterRead="afterRead"
					@delete="deletePic"
					name="1"
					width="200"
					height="200"
					uploadText="上传"
					:multiple="false"
					:maxCount="1"
				></u-upload>
			</view>
		</view>
		<view class="block">
			<!-- <view class="title">
				选择支付方式
			</view>
			<view class="content">
				<view class="pay-list">
					<view class="row" @tap="paytype='alipay'">
							<view class="left">
								<image src="/static/img/alipay.png"></image>
							</view>
							<view class="center">
								支付宝支付
							</view>
							<view class="right">
								<radio :checked="paytype=='alipay'" color="#f06c7a" />
							</view>
					</view>
					<view class="row" @tap="paytype='wxpay'">
							<view class="left">
								<image src="/static/img/wxpay.png"></image>
							</view>
							<view class="center">
								微信支付
							</view>
							<view class="right">
								<radio :checked="paytype=='wxpay'" color="#f06c7a" />
							</view>
					</view>
				</view>
			</view> -->
			
		</view>
		<view class="pay">
			<view class="btn" @tap="doDeposit">立即提交</view>
		<!-- 	<view class="tis">
				点击立即充值，即代表您同意<view class="terms">
					《条款协议》
				</view>
			</view> -->
		</view>
		<view class="content bg">
			<rich-text :nodes="rich"></rich-text>
		</view>
		<view class="block">
			<!-- <view class="title">
				账户信息
			</view> -->
			
		</view>
	</view>
</template>

<script>
	import service from '@/service.js';
	export default {
		data() {
			return {
				userinfo:{},
				money:{},
				inputAmount:'',//金额
				amountList:[1000,3000,5000,10000,30000,50000],//预设3个可选快捷金额
				paytype:'alipay',//支付类型
				fileList1: [],
				img:'',
				rich:'账户信息加载中'
			};
		},
		methods:{
			// 删除图片
				deletePic(event) {
					this[`fileList${event.name}`].splice(event.index, 1)
				},
				// 新增图片
				async afterRead(event) {
					// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
					let lists = [].concat(event.file)
					let fileListLen = this[`fileList${event.name}`].length
					lists.map((item) => {
						this[`fileList${event.name}`].push({
							...item,
							status: 'uploading',
							message: '上传中'
						})
					})
					for (let i = 0; i < lists.length; i++) {
						const result = await this.uploadFilePromise(lists[i].url)
						let item = this[`fileList${event.name}`][fileListLen]
						this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
							status: 'success',
							message: '',
							url: result
						}))
						fileListLen++
					}
				},
				uploadFilePromise(url) {
					return new Promise((resolve, reject) => {
						let a = uni.uploadFile({
							url: this.apiurl +'upload/index.html', // 仅为示例，非真实的接口地址
							filePath: url,
							name: 'file',
							formData: {
								user: 'test'
							},
							success: (res) => {
								//自定义返回数据赋值
								var result = JSON.parse(res.data);
								if(result.code == -1){
									this.$api.msg(result.msg);
								}else{
									console.log(result.data);
									
									// this.src = this.imgurl+result.data;
									this.img = result.data;//图片url
									// this.userimg1(result.data);
									// console.log(this.img);
								}
								//uview原逻辑，显示图片
								console.log(res);
								setTimeout(() => {
									resolve(res.data.data)
									console.log(this.fileList1);
								}, 1000)
							}
						});
					})
				},
			select(amount){
				this.inputAmount = amount;
			},
			async doDeposit(){
				if (parseFloat(this.inputAmount).toString() == "NaN") {
					uni.showToast({title:'请选择正确金额',icon:'none'});
					return ;
				}
				if(this.inputAmount<=0){
					uni.showToast({title:'请输入大于0的金额',icon:'none'});
					return ;
				}
				if(parseFloat(this.inputAmount).toFixed(2)!=parseFloat(this.inputAmount)){
					uni.showToast({title:'最多只能输入两位小数哦~',icon:'none'});
					return ;
				}
				if((this.inputAmount/1000)>this.money.sc_money){
					uni.showToast({title:'抵扣券不足~',icon:'none'});
					return ;
				}
				if(this.img == ''){
					uni.showToast({title:'请上传截图',icon:'none'});
					return ;
				}
				//模板原有方法
				// uni.showLoading({
				// 	title:'支付中...'
				// });
				// setTimeout(()=>{
				// 	uni.hideLoading();
				// 	uni.showToast({
				// 		title:'支付成功'
				// 	});
				// 	setTimeout(()=>{
				// 		uni.redirectTo({
				// 			url:'../../pay/success/success?amount='+this.inputAmount
				// 		});
				// 	},300);
				// },700)
				var data = {
					// sendmsg: this.remark,   //留言
					// deliveryPrice: this.deliveryPrice,
					// type:this.flag,
					// givemoney:this.deliveryPrice,
					// password1:e,
					money:this.inputAmount,
					img:this.img,
					// token:uni.getStorageSync('token'),
					// pt:this.pt,
					// ptid:this.ptid,
					// bankid:this.bankid
				}
				var result = (await this.$ajax.get('users/addApplyMoney.html',{data:data}));
				console.log(result);
				if(result.data.code == 1){
					uni.showModal({
						title:'提示',
						content:result.data.msg,
						showCancel:false,
						success() {
							uni.switchTab({
								url:'/pages/tabBar/user/user'
							})
						}
					})
				}else{
					uni.showModal({
						title:'提示',
						content:result.data.msg,
						showCancel:false,
					})
				}
			}
			
		},
		async onLoad() {
			var result = (await this.$ajax.get('advertisement/getggid.html?id=17'));
			console.log(result);
			if(result.data.code == 1){
				this.rich = result.data.data.content;
			}
		},
		async onShow(){
			var result = (await this.$ajax.get('users/getuserinfo.html'));
			console.log(result);
			if (result.data.code == 1) {
				service.updateUser(result.data.data);
			}
			this.userinfo=uni.getStorageSync("USERS_KEY");
			this.money = this.userinfo.moneyDetail;
			console.log(this.money);
		}
	}
</script>

<style lang="scss">
	.block{
		width: 94%;
		padding: 20upx 3%;
		.title{
			width: 100%;
			font-size: 34upx;
		}
		.content{
			.left{
				width: 100%;
				margin: 20rpx 0;
				display: flex;
				.box{
					width: 50%;
					font-size: 36upx;
					
					.num{
						width: 100%;
						height: 50upx;
						display: flex;
						justify-content: center;
						align-items: flex-end;
						color: #f9a453;
					}
					.text{
						width: 100%;
						display: flex;
						justify-content: center;
						color: #3d3d3d;
						font-size: 28upx;
					}
				}
			}
			.my{
				width: 100%;
				height: 60upx;
				display: flex;
				align-items: center;
				font-size: 30upx;
				// border-bottom: solid 1upx #eee;
			}
			.amount{
				width: 100%;
				
				.list{
					
					display: flex;
					justify-content: space-between;
					flex-wrap: wrap;
					// padding: 20upx 0;
					.box{
						margin-top: 30rpx;
						width: 30%;
						height: 120upx;
						display: flex;
						justify-content: center;
						align-items: center;
						border-radius: 10upx;
						box-shadow: 0upx 5upx 20upx rgba(0,0,0,0.05);
						font-size: 36upx;
						background-color: #f1f1f1;
						color: 333;
						&.on{
							background-color: #f06c7a;
							color: #fff;
						}
					}
				}
				.num{
					margin-top: 10upx;
					display: flex;
					justify-content: flex-end;
					align-items: center;
					.text{
						padding-right: 10upx;
						font-size: 30upx;
					}
					.input{
						width: 28.2vw;
						border-bottom: solid 2upx #999;
						
						justify-content: flex-end;
						align-items: center;
						input{
							margin: 0 20upx;
							height: 60upx;
							font-size: 30upx;
							color: #f06c7a;
							justify-content: flex-end;
							align-items: center;
						}
					}
				}
			}
			.pay-list{
				width: 100%;
				border-bottom: solid 1upx #eee;
				.row{
					width: 100%;
					height: 120upx;
					display: flex;
					align-items: center;
					.left{
						width: 100upx;
						flex-shrink: 0;
						display: flex;
						align-items: center;
						image{
							width: 80upx;
							height: 80upx;
						}
					}
					.center{
						width: 100%;
						font-size: 30upx;
					}
					.right{
						width: 100upx;
						flex-shrink: 0;
						display: flex;
						justify-content: flex-end;
					}
				}
			}
		}
	}
	.pay{
		margin-top: 20upx;
		width: 100%;
		display: flex;
		justify-content: center;
		flex-wrap: wrap;
		.btn{
			width: 70%;
			height: 80upx;
			border-radius: 80upx;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #fff;
			background-color: #f06c7a;
			box-shadow: 0upx 5upx 10upx rgba(0,0,0,0.2);
		}
		.tis{
			margin-top: 10upx;
			width: 100%;
			font-size: 24upx;
			display: flex;
			justify-content: center;
			align-items: baseline;
			color: #999;
			.terms{
				color: #5a9ef7;
			}
		}
	}
</style>
